package com.zkthink.ceres.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zkthink.base.R;
import com.zkthink.base.request.PageParams;
import com.zkthink.base.service.SuperServiceImpl;
import com.zkthink.ceres.finance.dao.DepositJournalMapper;
import com.zkthink.ceres.finance.dto.*;
import com.zkthink.ceres.finance.entity.DepositJournal;
import com.zkthink.ceres.finance.service.DepositJournalService;

import com.zkthink.ceres.finance.vo.CashOutDetailVO;
import com.zkthink.context.BaseContextHandler;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * <p>
 * 业务实现类
 * 
 * </p>
 *
 * @author vivi.huang
 * @date 2020-06-26
 */
@Slf4j
@Service
@AllArgsConstructor
public class DepositJournalServiceImpl extends SuperServiceImpl<DepositJournalMapper, DepositJournal> implements DepositJournalService {

    private DepositJournalMapper depositJournalMapper;

    @Override
    public R<IPage<DepositJournalDTO>> findDepositJournalPage(PageParams<DepositJournalPageDTO> pageParams) {
        IPage<DepositJournalDTO> page = pageParams.getPage();
        queryDepositJournal(pageParams, page);
        return R.success(page);
    }

    @Override
    public R<DepositSummaryDTO> getDepositSummary() {
        String tenant = getTenant();
        Long total =  this.depositJournalMapper.getDepositSummary(tenant);
        return R.success(new DepositSummaryDTO(total));
    }

    @Override
    public boolean updateRefundState(String transactionId) {
        this.depositJournalMapper.updateRefundState(transactionId);
        return true;
    }

    @Override
    public boolean saveDepositJournal(DepositJournalSaveDTO depositJournal) {
        this.depositJournalMapper.saveDepositJournal(depositJournal);
        return false;
    }

    private void queryDepositJournal(PageParams<DepositJournalPageDTO> params, IPage<DepositJournalDTO> page) {
        DepositJournalPageDTO pageDTO = params.getModel();

        String tenant = getTenant();
        Integer total = this.depositJournalMapper.getTotalOfDepositJournalPage(tenant,
                pageDTO.getStoreName(), pageDTO.getState(), pageDTO.getStartTime(), pageDTO.getEndTime());

        if (total > 0) {
            List<DepositJournalDTO> depositJournalDTOS = this.depositJournalMapper.findDepositJournalPage(tenant,
                    pageDTO.getStoreName(), pageDTO.getState(), pageDTO.getStartTime(), pageDTO.getEndTime(), (page.getCurrent() - 1), page.getSize());

            page.setRecords(depositJournalDTOS);
            page.setTotal(total);
        }
    }

    private String getTenant() {
        String tenant = BaseContextHandler.getTenant();
        if (StringUtils.isBlank(tenant) || tenant.equals("admin")) {
            return null;
        }
        return tenant;
    }


}
